package com.king.modules.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.king.common.utils.SecurityUtils;
import com.king.framework.base.controller.YyBaseController;
import com.king.framework.base.domain.vo.AjaxResult;
import com.king.framework.base.domain.vo.PageResult;
import com.king.modules.system.domain.SysGroupMenuEntity;
import com.king.modules.system.domain.SysMenuEntity;
import com.king.modules.system.domain.vo.GroupMenuDTO;
import com.king.modules.system.service.SysGroupMenuService;


/**
 * 集团菜单中间表 controller层
 * @author liusheng
 */
@RestController
@RequestMapping(value = "/system/groupMenu")
public class SysGroupMenuController extends YyBaseController<SysGroupMenuEntity>{

	Logger logger = LoggerFactory.getLogger(SysGroupMenuController.class);

	@Autowired
    private SysGroupMenuService groupMenuService;
	

	/**
	 * 查询菜单和按钮
	 * @param request
	 * @param groupid
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/queryMenuRes/{ownorgid}")
	public AjaxResult queryMenuRes(@PathVariable(value = "ownorgid") Long ownorgid) throws Exception {
		// 获取所有的菜单资源
		List<SysMenuEntity> list = groupMenuService.queryMenuRes(ownorgid);
        PageResult<SysMenuEntity> datas = new PageResult<>(list.size(), list);
        return success(datas);
	}
	
	

	// 分配权限（菜单）
	@PostMapping(value = "/auth")
	public AjaxResult auth(HttpServletRequest request, @RequestBody(required = false) GroupMenuDTO vo) throws Exception {
		// 分配权限
		groupMenuService.auth(vo);
		return AjaxResult.success("操作成功");
	}
	
	
	/**
	 * 获取已经授权的菜单和资源
	 * @param request
	 * @param groupid
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/queryOwnMenuRes")
	public AjaxResult queryOwnMenuRes(@RequestBody(required = false) SysMenuEntity form) throws Exception {
		// 获取所有的菜单资源
		form.setGroupid(SecurityUtils.getGroupid());
		List<SysGroupMenuEntity> list = groupMenuService.queryOwnMenuRes(form);
		return AjaxResult.success(new PageResult<>(list.size(), list));
	}
}
